package org.samith.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.samith.entity.Transaction;

public class TransactionWSClientServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ITransactionWS client = getClientWSProxy();
		Date fromDate = Calendar.getInstance().getTime();
		Date toDate = Calendar.getInstance().getTime();
		ArrayList<Transaction> transactions = (ArrayList<Transaction>) client
				.getTransactionsOfUser(4, fromDate, toDate);

		System.out.println("Server said 2 ======== : " + transactions.size());
		System.out.println("Server said 3 ======== : "
				+ transactions.get(0).getServiceId());
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	private ITransactionWS getClientWSProxy() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		factory.setServiceClass(ITransactionWS.class);
		factory.setAddress(getServletConfig().getInitParameter(
				"transactionServiceWSDLPath"));
		ITransactionWS client = (ITransactionWS) factory.create();
		return client;
	}
}
